package com.yun.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.reggie.common.CustomException;
import com.yun.reggie.dto.SetmealDto;
import com.yun.reggie.entity.Category;
import com.yun.reggie.entity.Setmeal;
import com.yun.reggie.entity.SetmealDish;
import com.yun.reggie.mapper.SetmealMapper;
import com.yun.reggie.service.CategoryService;
import com.yun.reggie.service.SetmealDishService;
import com.yun.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author xiao
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 先添加套餐setmeal，再添加套餐和菜品的关系setmealDish
     * @param setmealDto
     */
    @Override
    public void addWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);

        //给套餐-菜品类SetmealDish设置套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish:setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 批量删除套餐，还需要删除套餐-菜品
     * @param ids
     */
    @Override
    public void deleteWithDish(List<Long> ids) {
        //只能删除停售的套餐,统计这些套餐的状态值为1的个数，大于0说明，删除的套餐中有起售状态的
        //select count(*) from setmeal where setmeal_id in (1,2,3) and status = 1
        LambdaQueryWrapper<Setmeal> setMealWrapper = new LambdaQueryWrapper<>();
        setMealWrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        int count = this.count(setMealWrapper);
        if (count > 0){
            throw new CustomException("套餐正在售卖，不能删除！");
        }

        //根据setmealId批量删除套餐
        this.removeByIds(ids);

        //根据setmealId删除套餐-菜品，而不是根据setmealDishId
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(wrapper);
    }

    /**
     * 查询方法，不涉及事务
     * 根据套餐id，查询套餐信息，套餐-菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto queryByIdWithDish(Long id) {
        //查询套餐信息
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询套餐-菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(setmealDishes);

        //还需要根据categoryId查找套餐分类名
        Category category = categoryService.getById(setmealDto.getCategoryId());
        setmealDto.setCategoryName(category.getName());
        return setmealDto;
    }

    /**
     * 先修改套餐表，再根据套餐id删除套餐-菜品，然后添加套餐-菜品，以到达修改的目的
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐表
        this.updateById(setmealDto);

        //根据套餐id删除套餐-菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(wrapper);

        //添加套餐-菜品,注意套餐-菜品集合中，没有设置套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish:setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 批量修改套餐状态
     * @param ids
     */
    @Override
    public void updateWithStatus(List<Long> ids, int status) {
        //UPDATE setmeal SET status=? WHERE (id IN (?,?,?,?,?))
        /* QueryWrapper实现修改
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        this.update(setmeal,wrapper);
         */

        //方法2使用LambdaUpdateWrapper实现修改
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        //设置修改条件
        wrapper.in(Setmeal::getId,ids);
        //设置修改的字段
        wrapper.set(Setmeal::getStatus,status);
        this.update(wrapper);
    }
}
